package cloud.maque.admin.controller;

import cloud.maque.admin.domain.entity.MqXzq;
import cloud.maque.admin.domain.request.MqXzqQueryRequest;
import cloud.maque.admin.domain.response.MqXzqTree;
import cloud.maque.admin.service.MqXzqService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cloud.maque.framework.common.response.ResponseBodyVo;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行政区
 *
 * @author zhuyf
 * @date 2022/6/28 15:52
 */
@RestController
@RequestMapping("/xzq")
@RefreshScope //自动刷新nacos配置
@Api(value = "组件-行政区", tags = "组件-行政区")
public class XzqController {

    @Autowired
    MqXzqService compXzqService;

    /**
     * 行政区树
     *
     * @param parentcode 父级行政区代码(全国用0)
     * @param isnew      isnew :是否只显示最新数据，1就是最新，默认不取最新
     * @param getchild   是否获取下级所有行政区: true 为所有，默认不
     * @param haveself   返回时是否包含自身:1为包含,默认不包含
     * @param cache      是否使用缓存，1为使用
     * @return
     * @ignore
     */
    @GetMapping("/xzqtree")
    public ResponseBodyVo<List<MqXzqTree>> XzqTree(@RequestParam("parentcode") String parentcode,
                                                   @RequestParam(name = "isnew", required = false) String isnew,
                                                   @RequestParam(name = "getchild", required = false) Boolean getchild,
                                                   @RequestParam(name = "haveself", required = false) String haveself,
                                                   @RequestParam(name = "cache", required = false) String cache) {

        Boolean fromRedis = false;
        if (!StrUtil.isEmptyIfStr(cache)) {
            fromRedis = cache.equals("1") ? true : false;
        }
        List<MqXzqTree> result = compXzqService.xzqTree(parentcode, isnew, getchild, haveself, fromRedis);
        return ResponseBodyVo.Success("成功", result);
    }

    /**
     * 从缓存中读取行政区数据，该接口主要对外和业务层面
     *
     * @param parentcode 父级行政区代码(全国用0)
     * @param isnew      isnew :是否只显示最新数据，1就是最新，默认不取最新
     * @param getchild   是否获取下级所有行政区: true 为所有，默认不
     * @param haveself   返回时是否包含自身:1为包含,默认不包含
     * @return
     */
    @GetMapping("/xzqtreeWithCache")
    @ApiOperation(value = "获取行政区树")
    public ResponseBodyVo<List<MqXzqTree>> XzqTreeCache(@RequestParam(name = "parentcode") @ApiParam(value = "父级行政区代码(全国用0)", required = true) String parentcode,
                                                    @RequestParam(name = "isnew", required = false) @ApiParam(value = "是否只显示最新数据，1就是最新，默认不取最新") String isnew,
                                                    @RequestParam(name = "getchild", required = false) @ApiParam(value = "是否获取下级所有行政区: true 为所有，默认不)") Boolean getchild,
                                                    @RequestParam(name = "haveself", required = false) @ApiParam(value = "返回时是否包含自身:1为包含,默认不包含") String haveself) {


        List<MqXzqTree> result = compXzqService.xzqTree(parentcode, isnew, getchild, haveself, true);
        return ResponseBodyVo.Success("成功", result);
    }

    /**
     * 行政区列表
     *
     * @return
     * @ignore
     */
    @PostMapping("/xzqlist")
    public ResponseBodyVo<PageInfo<List<MqXzqTree>>> XzqList(@RequestBody PaginateDataResquest<MqXzqQueryRequest> queryRequest) {
        PageInfo<List<MqXzqTree>> result = compXzqService.listByPage(queryRequest);
        return ResponseBodyVo.Success("成功", result);
    }


    /**
     * 单个行政区信息
     *
     * @return
     * @ignore
     */
    @GetMapping("/detailById/{xzqId}")
    public ResponseBodyVo<MqXzq> DetailById(@PathVariable("xzqId") String xzqId) {
        MqXzq result = compXzqService.DetailById(xzqId);
        return ResponseBodyVo.Success("成功", result);
    }

    /**
     * 单个行政区信息
     *
     * @param xzqCode 行政区代码
     * @return
     * @ignore
     */
    @GetMapping("/detailByCode/{xzqCode}")
    @ApiOperation(value = "获取单个行政区")
    public ResponseBodyVo<MqXzq> DetailByCode(@PathVariable("xzqCode") @ApiParam(value = "行政区代码", required = true) String xzqCode) {
        MqXzq result = compXzqService.DetailByCode(xzqCode);
        return ResponseBodyVo.Success("成功", result);
    }


    /**
     * 保存行政区
     *
     * @return
     * @ignore
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存行政区")
    public ResponseBodyVo<MqXzq> Save(@RequestBody MqXzq xzq) {
        MqXzq result = compXzqService.Save(xzq);
        return ResponseBodyVo.Success("保存成功", result);
    }


    /**
     * 删除行政区
     *
     * @return
     * @ignore
     */
    @PostMapping("/delete/{xzqId}")
    @ApiOperation(value = "删除行政区")
    public ResponseBodyVo<Boolean> Delete(@PathVariable("xzqId") String xzqId) {
        Boolean result = compXzqService.Delete(xzqId);
        return ResponseBodyVo.Success("删除成功", result);
    }

    /**
     * 清除缓存
     *
     * @return
     * @ignore
     */
    @PostMapping("/clearCache")
    @ApiOperation(value = "清空缓存")
    public ResponseBodyVo ClearCache() {
        Boolean result = compXzqService.ClearCache();
        return ResponseBodyVo.Success("清除成功", result);
    }


}
